package com.bingxue.edu.tools;

import com.bingxue.edu.common.enums.ClassStatusEnum;
import com.bingxue.edu.common.enums.EnrollmentStatusEnum;
import com.bingxue.edu.common.enums.StudentClassEnum;
import com.bingxue.edu.management.education.model.entity.ClassInfo;
import com.bingxue.edu.management.education.service.ClassInfoService;
import com.bingxue.edu.management.education.service.ClassStudentService;
import com.bingxue.edu.management.education.service.EnrollmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ArrangeTemplateTableDef.ARRANGE_TEMPLATE;
import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.ClassStudentTableDef.CLASS_STUDENT;
import static com.bingxue.edu.management.education.model.entity.table.EnrollmentTableDef.ENROLLMENT;

/**
 * 处理重复缴费导致的订单状态异常
 * @author panbo
 * @since 2024/11/23
 */
@SpringBootTest
@Slf4j
public class ClassEnrollCountFixedTool {

    @Autowired
    private EnrollmentService enrollmentService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private ClassStudentService classStudentService;

    //    @Test
    public void test1() {
        List<String> ids2 = classInfoService.queryChain()
                .select(CLASS_INFO.ID)
                .leftJoin(ARRANGE_TEMPLATE).on(ARRANGE_TEMPLATE.ID.eq(CLASS_INFO.ARRANGE_TEMPLATE_ID))
                .where(CLASS_INFO.CLASS_STATUS.eq(ClassStatusEnum.TEACHING))
                .and(ARRANGE_TEMPLATE.END_DATE.lt(LocalDate.now()))
                .listAs(String.class);
        System.out.println(ids2);
    }

//    @Test
//    @Transactional(rollbackFor = Exception.class)
    public void fixed() {
        List<ClassInfo> list = classInfoService.queryChain()
                .where(CLASS_INFO.CLASS_YEAR.eq(2025))
                .and(CLASS_INFO.CLASS_STATUS.ne(ClassStatusEnum.CLOSED))
                .list();
        for (ClassInfo classInfo : list) {
            int classPassedStudentCount  = (int)classInfo.getPassedStudentCount();
            // class student
            int classStudentCount = (int)classStudentService.queryChain()
                    .where(CLASS_STUDENT.CLASS_ID.eq(classInfo.getId()))
                    .and(CLASS_STUDENT.STUDENT_CLASS_STATUS.eq(StudentClassEnum.NORMAL))
                    .count();
            int enrollmentCount = (int)enrollmentService.queryChain()
                    .where(ENROLLMENT.CLASS_ID.eq(classInfo.getId()))
                    .and(ENROLLMENT.STATUS.eq(EnrollmentStatusEnum.NORMAL))
                    .count();
            if (classPassedStudentCount != classStudentCount || classPassedStudentCount != enrollmentCount) {
                log.error("班级学员数量异常, classId: {}, classEnrolledStudentCount: {}, classPassedStudentCount: {}, classStudentCount: {}, enrollmentCount: {}", classInfo.getId(), classInfo.getEnrollStudentCount() ,classPassedStudentCount, classStudentCount, enrollmentCount);
            }
        }
    }


}
